package app.caihan.scframe.databind

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.annotation.Nullable
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import app.caihan.scframe.R
import com.blankj.utilcode.util.ClickUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * BindingAdapter的使用
 *
 * @author Seven-C
 * @date 2020/8/1
 */

@BindingAdapter(
    "imageUrl",
    "imagePlaceholder",
    requireAll = false
)
fun ImageView.loadImage(
    imageUrl: String,
    @DrawableRes imagePlaceholder: Int = R.drawable.ic_image_def
) {
    if (!imageUrl.isNullOrEmpty()) {
        Glide.with(this)
            .load(imageUrl)
            .placeholder(imagePlaceholder)
            .error(imagePlaceholder)
            .into(this)
    } else {
        setImageResource(imagePlaceholder)
    }
}

@BindingAdapter(value = ["circleImageUrl", "imagePlaceholder"], requireAll = false)
fun ImageView.circleImageUrl(
    url: String,
    @DrawableRes imagePlaceholder: Int = R.drawable.scframe_ic_empty
) {
    Glide.with(this)
        .load(url)
        .placeholder(imagePlaceholder)
        .error(imagePlaceholder)
        .apply(RequestOptions.bitmapTransform(CircleCrop()))
        .transition(DrawableTransitionOptions.withCrossFade(500))
        .into(this)
}

@BindingAdapter(value = ["visible"], requireAll = false)
fun View.visible(visible: Boolean) {
    visibility = if (visible) View.VISIBLE else View.GONE
}

@BindingAdapter(value = ["visible"], requireAll = false)
fun ImageView.visible(str: String) {
    visibility = if (str.isNullOrEmpty()) View.GONE else View.VISIBLE
}

@BindingAdapter(value = ["setVectorColor"], requireAll = false)
fun ImageView.setVectorColor(colorRes: Int) {
    drawable.setTint(ContextCompat.getColor(context, colorRes))
}

@BindingAdapter(value = ["showDrawable", "drawableShowed"], requireAll = false)
fun ImageView.showDrawable(showDrawable: Boolean, drawableShowed: Int) {
    setImageResource(if (showDrawable) drawableShowed else android.R.color.transparent)
}

@BindingAdapter(value = ["textColorRes"], requireAll = false)
fun TextView.setTextColor(textColorRes: Int) {
    setTextColor(ContextCompat.getColor(context, textColorRes))
//    setTextColor(getColorStateList(resources,textColorRes,null))
}

@BindingAdapter(value = ["imageRes"], requireAll = false)
fun setImageRes(imageView: ImageView, imageRes: Int) {
    imageView.setImageResource(imageRes)
}

@BindingAdapter(value = ["select"], requireAll = false)
fun selected(view: View, select: Boolean) {
    view.isSelected = select
}

@BindingAdapter(value = ["transitionName"], requireAll = false)
fun View.setTransitionName(transitionName: String) {
    ViewCompat.setTransitionName(this, transitionName)
}


@BindingAdapter(value = ["onClickWithDebouncing"], requireAll = false)
fun onClickWithDebouncing(view: View?, clickListener: View.OnClickListener?) {
    ClickUtils.applySingleDebouncing(view, clickListener)
}

@BindingAdapter(value = ["adjustWidth"])
fun adjustWidth(view: View, adjustWidth: Int) {
    val params = view.layoutParams
    params.width = adjustWidth
    view.layoutParams = params
}

@BindingAdapter(value = ["adjustHeight"])
fun adjustHeight(view: View, adjustHeight: Int) {
    val params = view.layoutParams
    params.height = adjustHeight
    view.layoutParams = params
}

@BindingAdapter(value = ["bindAdapter", "emptyViewId"], requireAll = false)
fun RecyclerView.bindAdapter(
    @Nullable adapter: BaseQuickAdapter<*, *>,
    @LayoutRes emptyViewId: Int = 0
) {
    setAdapter(adapter)
    adapter.recyclerView = this
    if (emptyViewId != 0) {
        adapter.setEmptyView(emptyViewId)
    }
}
